﻿using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using AvalonDock;
using DCT.ViewModel;

namespace DCT.View
{
    public class WorkspaceManagerAdapter
    {
        #region Constructor

        public WorkspaceManagerAdapter(DockingManager dockingManager)
        {
            this._avalonDockingManager = dockingManager;

            WorkspaceManager.Instance.ActiveDocumentChanged += this.OnWorkspaceManagerActiveWorkspaceChanged;
            WorkspaceManager.Instance.Workspaces.CollectionChanged += this.OnWorkspaceManagerCollectionChanged;

            //this._avalonDockingManager.PropertyChanged += this.DockingManagerPropertyChanged;
            this._avalonDockingManager.ActiveContentChanged += new System.EventHandler(_avalonDockingManager_ActiveContentChanged);
            //this._avalonDockingManager.ActiveDocumentChanged += new System.EventHandler(_avalonDockingManager_ActiveDocumentChanged);
            //this._avalonDockingManager.DocumentClosed += this.DockingManagerClosedDocument;
        }

        #endregion //Constructor

        #region Fields

        private DockingManager _avalonDockingManager;

        #endregion //Fields

        #region Methods

        private void OnWorkspaceManagerActiveWorkspaceChanged(object sender, ActiveWorkspaceChangedEventArgs args)
        {
            if (args.NewWorkspace != null)
            {
                DocumentContent viewDoc = this.FindViewForWorkspaceViewModel(args.NewWorkspace);

                if (_avalonDockingManager.ActiveDocument != viewDoc)
                {
                    _avalonDockingManager.ActiveDocument = viewDoc;
                }
            }
        }

        private void OnWorkspaceManagerCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
        {
            if (args.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (WorkspaceViewModel removed in args.OldItems)
                {
                    DocumentContent viewDoc = this.FindViewForWorkspaceViewModel(removed);
                    if (viewDoc != null && viewDoc.IsCloseable)
                    {
                        viewDoc.Close();
                    }
                }
            }

            if (args.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (WorkspaceViewModel added in args.NewItems)
                {
                    DocumentContent newViewDoc = this.CreateViewForWorkspaceViewModel(added);
                    //_avalonDockingManager.MainDocumentPane.Items.Insert(0, newViewDoc);
                    newViewDoc.Show(_avalonDockingManager);
                }
            }
        }

        private DocumentContent FindViewForWorkspaceViewModel(WorkspaceViewModel workspaceViewModel)
        {
            var workspaceViews = from d in _avalonDockingManager.Documents
                                 where d.Content == workspaceViewModel
                                 select d;

            return workspaceViews.FirstOrDefault();
        }

        private DocumentContent CreateViewForWorkspaceViewModel(WorkspaceViewModel workspaceViewModel)
        {
            DocumentContent viewDoc = new DocumentContent();

            //viewDoc.DataContext = workspaceViewModel;

            viewDoc.Content = workspaceViewModel;


            MultiBinding titleBinding = new MultiBinding();
            titleBinding.Converter = new DisplayNameToTitleConverter();

            Binding displayNameBinding = new Binding("DisplayName") { Source = workspaceViewModel };
            titleBinding.Bindings.Add(displayNameBinding);

            Binding isChangedBinding = new Binding("IsChanged") { Source = workspaceViewModel };
            titleBinding.Bindings.Add(isChangedBinding);

            viewDoc.SetBinding(AvalonDock.ManagedContent.TitleProperty, titleBinding);

            viewDoc.Closing += OnUserClosingDocument;

            return viewDoc;
        }

        private void OnUserClosingDocument(object sender, CancelEventArgs e)
        {
            DocumentContent viewDoc = sender as DocumentContent;

            if (viewDoc != null)
            {
                WorkspaceViewModel viewModelDoc = viewDoc.Content as WorkspaceViewModel;

                if (viewModelDoc != null)
                {
                    if (viewModelDoc.Close_Can())
                    {
                        viewModelDoc.Close_Execute();
                    }
                }
            }
        }

        //private void DockingManagerPropertyChanged(object sender, PropertyChangedEventArgs args)
        //{
        //    if (args.PropertyName == "ActiveDocument")
        //    {
        //        WorkspaceViewModel viewModelDoc = null;

        //        var activeViewDoc = _avalonDockingManager.ActiveDocument;
        //        if (activeViewDoc != null)
        //        {
        //            viewModelDoc = activeViewDoc.Content as WorkspaceViewModel;
        //        }

        //        if (viewModelDoc != null)
        //        {
        //            WorkspaceManager.Instance.ActiveWorkspace = viewModelDoc;
        //        }
        //    }
        //}

        private void _avalonDockingManager_ActiveContentChanged(object sender, System.EventArgs e)
        {
            if (_avalonDockingManager.ActiveDocument != null && _avalonDockingManager.ActiveDocument.IsActiveContent)
            {
                WorkspaceViewModel viewModelDoc = null;

                var activeViewDoc = _avalonDockingManager.ActiveDocument;
                if (activeViewDoc != null)
                {
                    viewModelDoc = activeViewDoc.Content as WorkspaceViewModel;
                }

                if (viewModelDoc != null)
                {
                    WorkspaceManager.Instance.ActiveWorkspace = viewModelDoc;
                }
            }
            else
            {
                if (WorkspaceManager.Instance.ActiveWorkspace != null)
                {
                    WorkspaceManager.Instance.ActiveWorkspace.IsActivated = false;
                    WorkspaceManager.Instance.ActiveWorkspace = null;
                }
            }
        }

        //void _avalonDockingManager_ActiveDocumentChanged(object sender, System.EventArgs e)
        //{
            
        //}
        #endregion
    }
}